package com.ubaer.zsycz.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lsw.Base.AdapterRefresh;
import com.lsw.pullableview.PullToRefreshLayout;
import com.lsw.pullableview.PullableListView;
import com.lsw.util.StringUtil;
import com.lsw.util.ToastUtil;
import com.ubaer.zsycz.R;
import com.ubaer.zsycz.ui.adapter.SearchAutoAdapter;
import com.ubaer.zsycz.ui.base.activity.BaseActivity;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;

import static com.ubaer.zsycz.ui.adapter.SearchAutoAdapter.SEARCH_HISTORY;

/**
 * Created by Administrator on 2018/9/26 0026.
 */

public class SearchActivity extends BaseActivity implements AdapterRefresh, View.OnClickListener {

    //历史记录
    @BindView(R.id.list_seach)
    ListView list_seach;
    @BindView(R.id.et_seach)
    EditText et_seach;
    @BindView(R.id.iv_seach)
    ImageView iv_seach;
    @BindView(R.id.tv_seach)
    TextView tv_seach;
    @BindView(R.id.iv_return)
    ImageView iv_return;
    SearchAutoAdapter searchAutoAdapter;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    protected void initInjector() {

        InputFilter[] emojiFilters = {emojiFilter};
        et_seach.setFilters(emojiFilters);
        searchAutoAdapter = new SearchAutoAdapter(mContext, 10, this);
        list_seach.setAdapter(searchAutoAdapter);
        et_seach.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                searchAutoAdapter.performFiltering(s);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        iv_seach.setOnClickListener(this);
        tv_seach.setOnClickListener(this);
        iv_return.setOnClickListener(this);
        list_seach.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mContext, SearchResultActivity.class);
                intent.putExtra("seach", searchAutoAdapter.mObjects.get(position).getContent());
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initEventAndData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_seach:
            case R.id.tv_seach:
                if (StringUtil.isNullOrEmpty(et_seach.getText().toString().trim())) {
                    ToastUtil.showToast(mContext, "请输入关键字");
                    return;
                }
                saveSearchHistory();
                Intent intent = new Intent(mContext, SearchResultActivity.class);
                intent.putExtra("seach", et_seach.getText().toString().trim());
                startActivity(intent);
                break;
            case R.id.iv_return:
                finish();
                break;
        }
    }

    @Override
    public void onRefreshAdapter(int position, int taskid) {

    }

    /*
* 保存搜索记录
*/
    private void saveSearchHistory() {
        String text = et_seach.getText().toString().trim();
        if (text.length() < 1) {
            return;
        }
        SharedPreferences sp = getSharedPreferences(SEARCH_HISTORY, Context.MODE_PRIVATE);
        String longhistory = sp.getString(SEARCH_HISTORY, "");
        String[] tmpHistory = longhistory.split(",");
        ArrayList<String> history = new ArrayList<String>(
                Arrays.asList(tmpHistory));
        if (history.size() > 0) {
            int i;
            for (i = 0; i < history.size(); i++) {
                if (text.equals(history.get(i))) {
                    history.remove(i);
                    break;
                }
            }
            history.add(0, text);
        }
        if (history.size() > 0) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < history.size(); i++) {
                //最多保存10条数据
                if (i == 10)
                    break;
                sb.append(history.get(i) + ",");
            }
            sp.edit().putString(SEARCH_HISTORY, sb.toString()).commit();
        } else {
            sp.edit().putString(SEARCH_HISTORY, text + ",").commit();
        }
        searchAutoAdapter.initSearchHistory();
        searchAutoAdapter.notifyDataSetChanged();
    }


    /**
     * 过滤表情
     */
    InputFilter emojiFilter = new InputFilter() {
        Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
                Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);

        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            Matcher emojiMatcher = emoji.matcher(source);
            if (emojiMatcher.find()) {
                ToastUtil.showToast(mContext, "不支持输入表情");
                return "";
            }
            return null;
        }
    };
}
